package com.greenline.search.report.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.SimpleTimeZone;

/**
 * Created with IntelliJ IDEA.
 * User: liulei
 * Date: 14-2-12
 * Time: 下午2:48
 * To change this template use File | Settings | File Templates.
 */
public class DateUtils {
    /**
     * get before log name array
     * param date: the base day, if null then date = today
     * @return
     */
    public static String[] getLastWeekLogNameArray(Date date, int count){
        String[] logNameArray = new String[count];
        if(date == null){
            date = new Date();
        }
        Calendar calendarTime = Calendar.getInstance();
        calendarTime.setTime(date);
        int dayOfWeek = calendarTime.get(Calendar.DAY_OF_WEEK);
        //获取上周周日，如果date是周日则减去7
        if(dayOfWeek > 1){
            calendarTime.add(Calendar.DAY_OF_YEAR, 1 - dayOfWeek);
        }else {
            calendarTime.add(Calendar.DAY_OF_YEAR, 0 - count);
        }

        Date sundayOfWeek = calendarTime.getTime();

        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        //获取上周七天格式化的结果
        for(int i = 1-count; i <= 0; i++){
            calendarTime.add(Calendar.DAY_OF_YEAR,i);
            logNameArray[i+count-1] = df.format(calendarTime.getTime());
            calendarTime.setTime(sundayOfWeek);
        }
        return logNameArray;
    }

    public static Date getLastWeekToday(Date today){
        Calendar todayCal = Calendar.getInstance();
        todayCal.setTime(today);
        todayCal.set(todayCal.get(Calendar.YEAR),todayCal.get(Calendar.MONTH),
                todayCal.get(Calendar.DATE)-7);
        return todayCal.getTime();
    }

    public static void main(String[] args) {
        String[] logNameArray = new String[30];
        logNameArray = getLastWeekLogNameArray(new Date(),131);
        for(String str : logNameArray){
            System.out.println(str);
        }

    }
}
